package weaver.interfaces.zhlx.duss.action.financemodule.t6;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.weaver.general.BaseBean;
import weaver.conn.RecordSet;
import weaver.general.Util;
import weaver.interfaces.workflow.action.Action;
import weaver.interfaces.zhlx.duss.utils.T6Util;
import weaver.soa.workflow.request.RequestInfo;

import java.util.HashMap;
import java.util.Map;

/**
 * @Title: ecology-idea
 * @Company: 泛微软件
 * @author: weaver.Dusongsong
 * @version: 1.0
 * @CreateDate: 2023-11-27
 * @Description: 财务收/付款认领流程归档后调用接口回传资金系统
 * @ModifyLog:
 **/
public class WeaCwsfkrllcAction extends BaseBean implements Action {
    @Override
    public String execute(RequestInfo requestInfo) {
        String requestid = requestInfo.getRequestid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        String src = requestInfo.getRequestManager().getSrc();
        writeLog(">>>>>WeaCwsfkrllcAction>>>>>requestid=" + requestid);
        try {
            String message = "调用资金系统接口返回:";
            RecordSet rs = new RecordSet();
            rs.executeQuery("select id from " + tablename + " where requestid=?", requestid);
            rs.next();
            String mainid = Util.null2String(rs.getString("id"));
            String skbj = "0";
            JSONArray paramList = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt1 where mainid=? and nvl(type,1) !=2", mainid);
            while (rs.next()) {
                JSONObject dataJson = new JSONObject();
                dataJson.put("BANK_SERIAL_ID", Util.null2String(rs.getString("wyid")));
                dataJson.put("CS_CODE", Util.null2String(rs.getString("khdm")));
                dataJson.put("CS_NAME", Util.null2String(rs.getString("khmc")));
                dataJson.put("CD_SIGN", skbj);
                dataJson.put("CORP_CODE", Util.null2String(rs.getString("gsdm")));
                dataJson.put("DETAIL_ID", Util.null2String(rs.getString("zjjhidwb")));
                dataJson.put("BUSINESS_DATE", Util.null2String(rs.getString("jyrq")));
                paramList.add(dataJson);
            }
            if (!paramList.isEmpty()) {
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaCwsfkrllcAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaCwsfkrllcAction>>>>>paramList=" + paramList);
                JSONObject resultJson = T6Util.employFundingPlan(paramList, dataMap);
                writeLog(">>>>>WeaCwsfkrllcAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                    String data = Util.null2String(retrunData.getString("DATA"));
                    message += Util.null2String(retrunData.getString("MESSAGE"));
                    JSONArray dataList = JSONArray.parseArray(data);
                    int count = 0;
                    for (int i = 0; i < dataList.size(); i++) {
                        JSONObject dataJson = dataList.getJSONObject(i);
                        String code1 = Util.null2String(dataJson.getString("CODE"));
                        String msg = Util.null2String(dataJson.getString("MSG"));
                        if (!"2".equals(code1)) {
                            message += msg + ";";
                            count++;
                        } else {
                            String bankSerialId = Util.null2String(dataJson.getString("BANK_SERIAL_ID"));
                            rs.executeUpdate("update " + tablename + "_dt1 set type=? where mainid=? and wyid=?", code1, mainid, bankSerialId);
                        }
                    }
                    if (count > 0) {
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            //0收1支
            String fkbj = "1";
            JSONArray paramList1 = new JSONArray();
            rs.executeQuery("select * from " + tablename + "_dt2 where mainid=? and nvl(type,1) !=2", mainid);
            while (rs.next()) {
                JSONObject dataJson = new JSONObject();
                dataJson.put("BANK_SERIAL_ID", Util.null2String(rs.getString("wyid")));
                dataJson.put("DETAIL_ID", Util.null2String(rs.getString("zjjhxxm")));
                dataJson.put("CD_SIGN", fkbj);
                dataJson.put("CORP_CODE", Util.null2String(rs.getString("gsdm")));
                dataJson.put("BUSINESS_DATE", Util.null2String(rs.getString("jyrq")));
                paramList1.add(dataJson);
            }
            if (!paramList1.isEmpty()) {
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("jkdymc", "WeaCwsfkrllcAction");
                dataMap.put("lcid", requestid);
                dataMap.put("dylc", requestid);
                dataMap.put("cz", src);
                writeLog(">>>>>WeaCwsfkrllcAction>>>>>paramList1=" + paramList1);
                JSONObject resultJson = T6Util.employFundingPlan(paramList1, dataMap);
                writeLog(">>>>>WeaCwsfkrllcAction>>>>>resultJson=" + resultJson);
                String code = Util.null2String(resultJson.getString("code"));
                if ("success".equals(code)) {
                    JSONObject retrunData = JSONObject.parseObject(resultJson.getString("data"));
                    message += Util.null2String(retrunData.getString("MESSAGE"));
                    String data = Util.null2String(retrunData.getString("DATA"));
                    JSONArray dataList = JSONArray.parseArray(data);
                    int count = 0;
                    for (int i = 0; i < dataList.size(); i++) {
                        JSONObject dataJson = dataList.getJSONObject(i);
                        String code1 = Util.null2String(dataJson.getString("CODE"));
                        String msg = Util.null2String(dataJson.getString("MSG"));
                        if (!"2".equals(code1)) {
                            message += msg + ";";
                            count++;
                        } else {
                            String bankSerialId = Util.null2String(dataJson.getString("BANK_SERIAL_ID"));
                            rs.executeUpdate("update " + tablename + "_dt2 set type=? where mainid=? and wyid=?", code1, mainid, bankSerialId);
                        }
                    }
                    if (count > 0) {
                        requestInfo.getRequestManager().setMessageid("111111");
                        requestInfo.getRequestManager().setMessagecontent(message);
                        return Action.FAILURE_AND_CONTINUE;
                    }
                } else {
                    message += Util.null2String(resultJson.getString("message"));
                    requestInfo.getRequestManager().setMessageid("111111");
                    requestInfo.getRequestManager().setMessagecontent(message);
                    return Action.FAILURE_AND_CONTINUE;
                }
            }
            return Action.SUCCESS;
        } catch (Exception e) {
            writeLog(">>>>>WeaCwsfkrllcAction>>>>>Exception=" + e);
            requestInfo.getRequestManager().setMessageid("111111");
            requestInfo.getRequestManager().setMessagecontent("Action接口处理异常,请联系管理员");
            return Action.FAILURE_AND_CONTINUE;
        }
    }

}